<template>
  <div class="register-container">
    <div class="register-form-container">
      <h2 class="register-title">用户注册</h2>
      
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        autocomplete="on"
        label-position="left"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="用户名"
            type="text"
            autocomplete="username"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            placeholder="密码"
            type="password"
            autocomplete="new-password"
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            placeholder="确认密码"
            type="password"
            autocomplete="new-password"
          />
        </el-form-item>
        
        <el-button
          :loading="loading"
          type="primary"
          class="register-button"
          @click="handleRegister"
        >注册</el-button>
        
        <div class="login-link">
          <span>已有账号？</span>
          <router-link to="/login">登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Register',
  setup() {
    const registerFormRef = ref(null)
    const store = useStore()
    const router = useRouter()
    
    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: ''
    })
    
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码不能少于6个字符'))
      } else {
        callback()
      }
    }
    
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { validator: validatePass, trigger: 'blur' }
      ],
      confirmPassword: [
        { validator: validatePass2, trigger: 'blur' }
      ]
    }
    
    const loading = ref(false)
    
    const handleRegister = async () => {
      try {
        // 表单验证
        await registerFormRef.value.validate()
        
        loading.value = true
        
        // 准备注册数据
        const userData = {
          username: registerForm.username,
          password: registerForm.password
        }
        
        await store.dispatch('register', userData)
        
        ElMessage({
          message: '注册成功，即将跳转到首页',
          type: 'success'
        })
        
        router.push({ path: '/' })
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error(error.response?.data?.message || '注册失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
    
    return {
      registerFormRef,
      registerForm,
      registerRules,
      loading,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;
}

.register-form-container {
  width: 350px;
  padding: 30px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.register-form {
  margin-bottom: 20px;
}

.register-button {
  width: 100%;
  margin-top: 10px;
}

.login-link {
  margin-top: 20px;
  text-align: center;
}

.login-link a {
  color: #409EFF;
  text-decoration: none;
}
</style>
